const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const path = require('path')
// Webpack的使用环境
const flag = process.env.NODE_ENV === 'production'
module.exports = {
  // 打包模式
  mode: 'development',
  // sourcem-map
  devtool: flag ? 'nosources-source-map' : 'eval-cheap-module-source-map',
  // 入口
  entry: './src/index.js',
  // 出口
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  // 插件
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html'
    }),
    new MiniCssExtractPlugin({
      filename: 'css/[name].css'
    }),
    new CleanWebpackPlugin(),
    // 确保加入了这个插件
    new VueLoaderPlugin()
  ],
  // 模块
  module: {
    rules: [
      {// css
        test: /\.css$/i,
        use: [
          flag ? MiniCssExtractPlugin.loader : 'style-loader',
          'css-loader',
          'postcss-loader']
      }, {// less
        test: /\.less$/i,
        use: [
          flag ? MiniCssExtractPlugin.loader : 'style-loader',
          'css-loader',
          'postcss-loader',
          'less-loader']
      }, {// 图片
        test: /\.(png|jpe?g|gif|webp|svg)$/i,
        type: 'asset',
        generator: {
          filename: 'image/[hash:18]-[name][ext]'
        }
      }, {// 字体
        test: /\.(eot|woff2?|ttf|svg)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'font/[name][ext]'
        }
      }, {// babel降级js
        test: /\.m?jsx?$/,
        use: {
          loader: 'babel-loader'
        }
      }, {// 处理vue文件
        test: /\.vue$/i,
        use: 'vue-loader'
      }
    ]
  },
  // 开发服务器
  devServer: {
    port: 3000,
    open: true,
    client: {
      logging: 'none'
    }
  }
}

